/**
 * Created by Lin on 2018-1-3.
 */
const express=require('express');
const router=express.Router();
const testToken=require('./testToken');
const Income = global.dbHandel.getModel('income');

router.get('/incomeEdit',function (req,res) {
  let token=req.query.token;
  let id=req.query.id;
  let search={_id:id};
  let result=testToken.testToken(token);
  if(result.success==true){
    Income.findOne(search,function (error,docs) {
      if(error){
        res.send(500,{error:'获取信息失败'});
      }else {
        res.send(200,{data:docs})
      }
    })
  }else {
    res.send(500,{error:result});
  }
})
router.post('/incomeEditSubmit',function (req,res) {
  let user_id = req.body.user_id;
  let _id=req.body._id;
  let incomeMoney=req.body.incomeMoney;
  let incomeCategory=req.body.incomeCategory;
  let incomeAccount=req.body.incomeAccount;
  let incomeDate=req.body.incomeDate;
  let incomeTime=req.body.incomeTime;
  let recordTime=new Date(incomeDate+'-'+incomeTime);
  let description=req.body.description;
  let category_logo=req.body.category_logo;
  let account_logo=req.body.account_logo;
  const token=req.body.token;
  let submit={
    user_id:user_id,
    incomeMoney:incomeMoney,
    incomeCategory:incomeCategory,
    incomeAccount:incomeAccount,
    incomeDate:incomeDate,
    incomeTime:incomeTime,
    recordTime:recordTime,
    description:description,
    category_logo:category_logo,
    account_logo:account_logo
  }
  let result=testToken.testToken(token);
  if(result.success==true) {
    Income.update({_id:_id},submit,function (error,docs) {
      if(error){
        res.send(500,{error:'更改失败'});
      }else{
        res.send(200,{success:'更改成功'});
      }
    })
  }else {
    res.send(500,{error:result})
  }

})
module.exports=router;
